const CopyWebpackPlugin = require("copy-webpack-plugin");
const webpack = require("webpack");
const path = require("path");
const CompressionPlugin = require("compression-webpack-plugin");
const BundleAnalyzerPlugin =
  require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
//解除监听器限制
require("events").EventEmitter.defaultMaxListeners = 0;
let devServerProxy;

const isProduction = process.env.NODE_ENV !== "production"; //production:生产   development：开发
// let cesiumSource = "./cesium/Source";
// let cesiumWorkers = "Workers";
module.exports = {
  transpileDependencies: ['/@tmcw/togeojson/'],
  css: {
    loaderOptions: {
      less: {
        lessOptions: {
          modifyVars: {
            "primary-color": "#2688ED",
            "link-color": "#2688ED",
            "border-radius-base": "2px",
          },
          javascriptEnabled: true,
        },
      },
    },
    extract: false,
    sourceMap: false,
  },

  publicPath: "",
  lintOnSave: false,

  devServer: {
    port: 80, //修改服务端口号
    proxy: devServerProxy,
  },

  configureWebpack: {
    devtool: "source-map",
    output: {
      sourcePrefix: "",
    },
    amd: {
      toUrlUndefined: true,
    },
    resolve: {
      alias: {
        vue$: "vue/dist/vue.esm.js",
        "@": path.resolve("src"),
        "net": path.resolve("src/views/js/net"),
        // cesium: path.resolve(__dirname, cesiumSource),
      },
    },
    plugins: [
      new webpack.DefinePlugin({
        CESIUM_BASE_URL: JSON.stringify("./"),
      }),
      // new BundleAnalyzerPlugin()
    ],
    module: {
      unknownContextCritical: /^.\/.*$/,
      unknownContextCritical: false,
      rules: [
        {
          test: /\.glsl$/,
          loader: "webpack-glsl-loader", //用于加载.glsl格式的loader
        },
        {
          test: /\.worker\.js$/,
          use: {
            loader: "worker-loader",
            options: { inline: "fallback" },
          },
        },
      ],
    },
  },
  parallel: false, // 解决打包worker.js报错的问题
  chainWebpack: (config) => {
    config.plugins.delete("prefetch");
    config
      .plugin("ContextReplacementPlugin")
      .use(webpack.ContextReplacementPlugin, [/moment[/\\]locale$/, /zh-cn/]);
    config.plugin("provide").use(webpack.ProvidePlugin, [
      {
        $: "jquery",
        jquery: "jquery",
        jQuery: "jquery",
        "window.jQuery": "jquery",
      },
    ]);
    if (!isProduction) {
      // 添加分析工具
      if (process.env.npm_config_report) {
        config
          .plugin("webpack-bundle-analyzer")
          .use(require("webpack-bundle-analyzer").BundleAnalyzerPlugin)
          .end();
      }
      //gzip压缩
      config.plugin("compressionPlugin").use(
        new CompressionPlugin({
          filename: "[path].gz[query]",
          algorithm: "gzip",
          test: /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i,
          threshold: 10240,
          minRatio: 0.8,
          deleteOriginalAssets: false,
        })
      );
    }
    config.module.rule('md')
      .test(/\.md/)
      .use('vue-loader')
      .loader('vue-loader')
      .end()
      .use('vue-markdown-loader')
      .loader('vue-markdown-loader/lib/markdown-compiler')
      .options({
        raw: true
    })
  },

  productionSourceMap: false,
   // 第三方插件配置
   pluginOptions: {
    'style-resources-loader': {
        preProcessor: 'less',
        patterns: [
         path.resolve(__dirname, "./src/views/components/less/com-css.less") // 这段是自己加的，根据自己文件的位置来修改,用于添加全局样式表
        ]
    }
}
};
